#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n,s;
    cin>>n>>s;
    int t=max(1ll,n/(s+2)),ans=s-1;
    if(s==1)cout<<0<<endl;
    else
    {
        while(t!=s)
        {
            int x=abs(s-n/t)+t;
            if(x<ans)
            {
                ans=x;
            }
            if(s-2>=n/t)
            {
                break;
            }
            t++;
        }
        cout<<ans<<endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}